﻿@{
    var companys = ViewData["Companys"] as List<Wicresoft.UnifyShow.Business.SiteBranchModel>;
    var isGlobalUser = (bool)ViewData["IsGlobalUser"];
    var myCompany = ViewData["CurrentCompany"] as Wicresoft.UnifyShow.Business.SiteBranchModel;
}
<form>
    <div id="searchParam">
        <div>
            <div class="left">告警单位</div>
            <div class="right">
                <select data-source="CompanyName">
                    @if (!isGlobalUser)
                    { 
                        <option value="@myCompany.Name">@myCompany.Name</option>
                    }
                    else
                    {
                        <option value="">全部</option>
                        foreach (var item in companys)
                        { 
                        <option value="@item.Name">@item.Name</option>
                        }
                    }
                </select>
            </div>
        </div>
        <div>
            <div class="left">告警摘要/描述</div>
            <div class="right">
                <input type="text" data-source="Message" />
            </div>
        </div>
        <div>
            <div class="left">告警业务系统</div>
            <div class="right">
                <input type="text" data-source="BusinessName" />
            </div>
        </div>
        <div>
            <div class="left">告警开始时间从</div>
            <div class="right">
                <input type="text" class="easyui-datebox" data-source="CreateTime_From" />
            </div>
        </div>
        <div>
            <div class="left">到</div>
            <div class="right">
                <input type="text" class="easyui-datebox" data-source="CreateTime_To" />
            </div>
        </div>
        <div>
            <div class="left">告警分类</div>
            <div class="right">
                <select data-source="Type">
                    <option value="">全部</option>
                    <option value="服务器">服务器</option>
                    <option value="数据库">数据库</option>
                    <option value="中间件">中间件</option>
                    <option value="应用系统">应用系统</option>
                    <option value="网络">网络</option>
                    <option value="安全事件">安全事件</option>
                    <option value="机房">机房</option>
                    <option value="存储">存储</option>
                </select>
            </div>
        </div>
        <div>
            <div class="left">告警级别</div>
            <div class="right">
                <select data-source="Severity">
                    <option value="">全部</option>
                    <option value="严重">严重</option>
                    <option value="主要">主要</option>
                    <option value="一般">一般</option>
                    <option value="提醒">提醒</option>
                </select>
            </div>
        </div>
        <div>
            <div class="left">告警状态</div>
            <div class="right">
                <select data-source="Status">
                    <option value="">全部</option>
                    <option value="开启">开启</option>
                    <option value="关闭">关闭</option>
                </select>
            </div>
        </div>
        <div>
            <div class="left">告警IP地址</div>
            <div class="right">
                <input type="text" data-source="HostAddress" />
            </div>
        </div>
        <div>
            <div class="left">统计时间从</div>
            <div class="right">
                <input type="text" class="easyui-datebox" data-source="CalculateDate_From" />
            </div>
        </div>
        <div>
            <div class="left">到</div>
            <div class="right">
                <input type="text" class="easyui-datebox" data-source="CalculateDate_To" />
            </div>
        </div>
        <div></div>
        <div>
            <div class="left">无故障率(%)</div>
            <div class="right">
                <input type="text" data-source="UseagePercent_From" />
            </div>
        </div>
        <div>
            <div class="left">到</div>
            <div class="right">
                <input type="text" data-source="UseagePercent_To" />
            </div>
        </div>
        <div class="one-line">
            <div class="left">
                <button type="button" onclick="groupAlertList();">查询</button>
                <button type="reset">重置</button>
            </div>
            <div class="right"></div>
        </div>
    </div>
</form>
<div class="reportListContainer">
    <div class="configButton">
        <button data-querytype="告警综合" data-groupcode="" data-groupname="">报表配置</button>
        <iframe name="exportFrame" id="exportFrame" src="/ReportView/ExportReportViewToExcel" border="0" frameborder="0" style="border: none; height: 30px; vertical-align: middle; display: none;"></iframe>
    </div>
    <div class="reportList">
        <div style="height: 30%;" id="searchGroupListContainer">
            <table id="searchGroupList"></table>
        </div>
        <div style="height: 65%; margin-top: 10px;" id="searchListContainer">
            <table id="searchList"></table>
        </div>
    </div>
</div>
<div class="bgWrapper">
    <div id="dimensionSetting">
        <div style="height: 30px; line-height: 30px;">
            汇总维度：
            <select id="groupList">
                <option value="Type">告警类别</option>
                <option value="HostAddress">告警对象</option>
            </select>
        </div>
        <div><span class="leftTip">所有列：</span><span class="rightTip">显示列：</span></div>

        <div class="multipleSelect">
            <select multiple="multiple" id="allSelectable">
            </select>
            <div class="operation">

                <div>
                    <button id="add">添加 ></button>
                </div>
                <div>
                    <button id="addAll">全部添加 >></button>
                </div>
                <div>
                    <button id="remove">&lt; 移除</button>
                </div>
                <div>
                    <button id="removeAll">&lt;&lt; 全部移除 </button>
                </div>
            </div>
            <select multiple="multiple" id="selected">
            </select>

        </div>
        <div style="margin: 10px 0;">
            <button class="update">确定</button>
            <button class="cancel">取消</button>
        </div>
    </div>
</div>


<script type="text/javascript">

    var today = new Date();
    var defaultStartDate = today.getFullYear() + "-01-01",
        defaultEndDate = today.formate();

    //enable==true:以告警对象分组
    var enableUsagePercent = function (enable) {
        $('[data-source="CreateTime_From"]').datebox({ 'disabled': enable });
        $('[data-source="CreateTime_To"]').datebox({ 'disabled': enable });
        $('[data-source="CalculateDate_From"]').datebox({ 'disabled': !enable });
        $('[data-source="CalculateDate_To"]').datebox({ 'disabled': !enable });
        if (enable) {
            $('[data-source="UseagePercent_From"]').removeAttr('disabled');
            $('[data-source="UseagePercent_To"]').removeAttr('disabled');
            if (!$('[data-source="CalculateDate_From"]').datebox('getValue')) {
                $('[data-source="CalculateDate_From"]').datebox('setValue', defaultStartDate);
            }

            if (!$('[data-source="CalculateDate_To"]').datebox('getValue')) {
                $('[data-source="CalculateDate_To"]').datebox('setValue', defaultEndDate);
            }

            $('[data-source="CreateTime_From"]').datebox('setValue', '');
            $('[data-source="CreateTime_To"]').datebox('setValue', '');

            //以告警对象分组时，去除一般和提醒
            $('[data-source="Severity"] option[value="提醒"]').remove();
            $('[data-source="Severity"] option[value="一般"]').remove();

        } else {
            $('[data-source="UseagePercent_From"]').attr('disabled', true);
            $('[data-source="UseagePercent_To"]').attr('disabled', true);
            $('[data-source="CalculateDate_From"]').datebox('setValue', '');
            $('[data-source="CalculateDate_To"]').datebox('setValue', '');

            if ($('[data-source="Severity"] option[value="一般"]').length == 0)
                $('[data-source="Severity"]').append('<option value="一般">一般</option>');
            if ($('[data-source="Severity"] option[value="提醒"]').length == 0)
                $('[data-source="Severity"]').append('<option value="提醒">提醒</option>');
        }
    };

    $(function () {
        $('.easyui-datebox').datebox({
            formatter: function (date) {
                return date.formate('yyyy-MM-dd')
            }
        });

        $('.bgWrapper .update').die('click').live('click', function () {
            var queryType = $('.configButton button').attr('data-querytype');
            updateSelectedColumns(queryType, function () {
                $('.bgWrapper').hide();

                enableUsagePercent($('#groupList option:selected').text() == "告警对象");

                groupAlertList();

            });
        });

        getMySelectedColumns('告警综合', function (groupName) {
            enableUsagePercent(groupName == "告警对象");
        });
    });

    var groupAlertList = function () {

        enableButton(false);
        $('#exportFrame').hide();

        var validate = true;

        getMySelectedColumns('告警综合', function (groupName) {

            //如果按告警对象分组统计，则要求填写统计时间和告警分类
            if (groupName == "告警对象") {
                if (!$('[data-source="CalculateDate_From"]').datebox('getValue') || !$('[data-source="CalculateDate_To"]').datebox('getValue')) {
                    alert('以告警对象作为维度时,请填写统计开始时间和结束时间！');
                    enableButton(true);
                    return;
                }
            }

            //如果填写了无故障率，则必须填写统计时间
            if ($('[data-source="UseagePercent_From"]').val() || $('[data-source="UseagePercent_To"]').val()) {
                if (!$('[data-source="CalculateDate_From"]').datebox('getValue') || !$('[data-source="CalculateDate_To"]').datebox('getValue')) {
                    alert('计算无故障率时,请填写统计开始时间和结束时间！');
                    enableButton(true);
                    return;
                }
            }

            var container = $('#searchParam');
            var query = ReportViewInsp.getQueryData(container);
            if (groupName == "告警对象") {
                query["CreateTime_From"] = query["CalculateDate_From"];
                query["CreateTime_To"] = query["CalculateDate_To"];
            }

            $.ajax({
                url: "/ReportView/GroupAlertList",
                type: "POST",
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify({
                    query: query
                }),
                success: function (res) {

                    if (res && res.rows) {

                        //设置维度值
                        $('[data-groupCode]').attr('data-groupCode', res.groupCode);
                        $('[data-groupName]').attr('data-groupName', res.groupName);

                        var sampleColumns = [
                            { field: 'ck', title: '全选', checkbox: true },
                            { field: res.groupCode, title: '汇总维度(' + res.groupName + ')', width: 120 },
                            { field: 'Count', title: '计数', width: 120 }
                        ];

                        if (groupName == "告警对象") {
                            sampleColumns.push({
                                field: "WorkTime", title: "无故障运行时间(H)", width: 120
                            });
                            sampleColumns.push({
                                field: "UsagePercent", title: "无故障率", width: 120
                            });

                            sampleColumns.splice(2, 0, { field: "ItemName", title: "主机名称", width: 120 })
                        } else {
                            //告警对象分组没有合计
                            var total = {};
                            total[res.groupCode] = '总计';
                            total['Count'] = res.total;
                            res.rows.push(total);
                        }

                        $('#searchGroupList').datagrid({
                            pagination: false,
                            fit: true,
                            fitColumns: true,
                            columns: [sampleColumns],
                            singleSelect: false,
                            data: res,
                            onLoadSuccess: function () {

                                $('#exportFrame').show();

                                enableButton(true);
                            },
                            onCheck: function (index, row) {
                                searchAlertList(ReportViewInsp.allQueryData(container, 'searchGroupList', res.groupCode), row);
                            },
                            onUncheck: function (index, row) {
                                searchAlertList(ReportViewInsp.allQueryData(container, 'searchGroupList', res.groupCode), row);
                            },
                            onCheckAll: function (rows) {
                                searchAlertList(ReportViewInsp.allQueryData(container, 'searchGroupList', res.groupCode), rows);
                            },
                            onUncheckAll: function (rows) {
                                searchAlertList(ReportViewInsp.allQueryData(container, 'searchGroupList', res.groupCode), rows);
                            }
                        });

                        if ($('#searchGroupListContainer').find('table tr.datagrid-row').length > 0) {
                            //$('#searchGroupListContainer').find('table tr.datagrid-row').first().click();
                        }
                        else $('#searchList').datagrid('loadData', { total: 0, rows: [] });
                    }
                }
            });
        });
    };

    var searchAlertList = function (queryData, rows) {
        var container = $('#searchParam');
        var query = queryData || ReportViewInsp.getQueryData(container);

        if ($('[data-groupname]').attr('data-groupname') == "告警对象") {
            query["CreateTime_From"] = query["CalculateDate_From"];
            query["CreateTime_To"] = query["CalculateDate_To"];
        }
        enableButton(false);
        $('#exportFrame').hide();
        $.ajax({
            url: '/ReportView/GetMySelectedColumns',
            data: {
                QueryType: "告警综合"
            },
            success: function (res) {

                var selected = res.selectedColumns,
                    columns = [];

                $.each(selected, function (i, item) {
                    columns.push({ field: item.Field, title: item.Name, sortable: true, formatter: DateFormate });
                });

                var url = '/ReportView/SearchAlertList';

                $('#searchList').datagrid({
                    url: url,
                    pagination: true,
                    fit: true,
                    pageList: [10, 20],
                    pageSize: 20,
                    pageNumber: 1,
                    queryParams: query,
                    singleSelect: true,
                    sortName: 'CreateTime',
                    sortOrder: 'desc',
                    columns: [columns],
                    onLoadSuccess: function () {

                        $('#exportFrame').show();

                        enableButton(true);
                    }
                });

                var p2 = $('#searchList').datagrid('getPager');
                $(p2).pagination({
                    beforePageText: '第',
                    afterPageText: '页&nbsp;共&nbsp;{pages}&nbsp;页',
                    displayMsg: '当前显示{from} - {to}&nbsp;条记录&nbsp;共&nbsp;{total}&nbsp;条记录'

                });
            }
        })
    };
</script>
